package com.anne.fish.webapp.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.anne.fish.model.Area;
import com.anne.fish.model.Cat;
import com.anne.fish.model.Info;
import com.anne.fish.model.InfoImage;
import com.anne.fish.model.SearchWord;
import com.anne.fish.model.User;
import com.anne.fish.service.AreaManager;
import com.anne.fish.service.CatManager;
import com.anne.fish.service.InfoImageManager;
import com.anne.fish.service.InfoManager;
import com.anne.fish.service.SearchWordManager;
import com.anne.fish.webapp.formbean.InfoSearch;
import com.opensymphony.xwork2.Preparable;

public class InfoAction extends BaseAction implements Preparable {
	private InfoManager infoManager;
	private List<Info> infoes;
	private Info info;
	private Long id;
	private String query;

	private AreaManager areaManager;
	private List<Area> areas;
	private CatManager catManager;
	private SearchWordManager searchWordManager;
	private InfoImageManager infoImageManager;
	private List<InfoImage> infoImages;

	private User infoUser;
	private InfoSearch is;
	private Cat cat;
	private Cat fatherCat;
	
	private List<Cat> cats;
	
	private Area area;
	
	


	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	/**
	 * Grab the entity from the database before populating with request
	 * parameters
	 */
	public void prepare() {
		if (getRequest().getMethod().equalsIgnoreCase("post")) {
			// prevent failures on new
			String infoId = getRequest().getParameter("info.id");
			if (infoId != null && !infoId.equals("")) {
				info = infoManager.get(new Long(infoId));
			}
		}
	}

	public void setQ(String q) {
		this.query = q;
	}

	public String list() {
		// infoes = infoManager.search(query, Info.class);
		if(is !=null){
			if(is.getCatId()!=null && is.getCatId()!= InfoSearch.DEFAULT_VALUE_ALL_INT){
				cat = this.catManager.get(is.getCatId().longValue());
				// father cat
				fatherCat = this.showAreaById(cat.getParentId());
				cats = fatherCat.getSubCats();
			}else{
				is.setCatId(InfoSearch.DEFAULT_VALUE_ALL_INT); //意思是在页面显示全部
			}
			if(is.getAreaId()!=null && is.getAreaId()!= InfoSearch.DEFAULT_VALUE_ALL_INT){
				area = this.showProvince(is.getAreaId());
			}else{
				is.setAreaId(InfoSearch.DEFAULT_VALUE_ALL_INT);
			}

			
		}else{
			is = new InfoSearch();
			is.setCatId(InfoSearch.DEFAULT_VALUE_ALL_INT); //意思是在页面显示全部
			
			is.setAreaId(InfoSearch.DEFAULT_VALUE_ALL_INT);
		}

		if(is.getPic() ==null  ){
			is.setPic(InfoSearch.DEFAULT_VALUE_ALL_INT);
		}
		
		areas = this.getProvinces();
		
		
		Integer count = this.infoManager.getCountBySearch(is);
		is.setPageTotalRow(count);
		is.setPageCurrentPage(getPageCurrentPage());
		if (count > 0) {
			infoes = infoManager.findBySearch(is);
		}

		if (!StringUtils.isBlank(is.getSearchWord())) {
			SearchWord searchWord = new SearchWord();
			searchWord.setSearchDate(System.currentTimeMillis());
			searchWord.setSearchKeyword(is.getSearchWord());
			searchWord.setSearchEngine("");
			searchWordManager.save(searchWord);
		}

		return SUCCESS;
	}

	public String listByUser() {

		if (is == null) {
			is = new InfoSearch();
			is.setUid(getUserId());
		}

		return list();
	}
	public String get() {
		if (id == null) {
			return INPUT;
		}
		is = new InfoSearch();
		//is.toString();
		
		info = infoManager.get(id);
		info.setVisitNum(info.getVisitNum() + 1);
		// future add to q
		info = infoManager.save(info);
		if (info.isPicFlag()) {
			this.infoImages = this.infoImageManager.findByInfoId(id);
		}
		this.infoUser = this.userManager.get(info.getUid());		
		//该info的用户发布的其他infoes，最多10条
		
		is.setUid(info.getUid());
		is.setPageRowPerPage(5);
		
		
		list();

		return SUCCESS;
	}




	public String edit() {

		this.areas = this.areaManager.findByParentId(0L);
		this.cats = this.catManager.findByFatherId(0L);
		
		
		
		if (id != null) {
			info = infoManager.get(id);
			if (info.isPicFlag()) {
				this.infoImages = this.infoImageManager.findByInfoId(id);
			}
		} else {
			info = new Info();
			info.setSellFlag(true);
			// 默认为转让

		}

		return SUCCESS;
	}

	public String delete() {
		infoManager.remove(id); 

		return SUCCESS;
	}

	public String save() throws Exception {

		boolean isNew = (info.getId() == null);
		infoImages = convert(infoImages);
		int img_size = (infoImages == null) ? 0 : infoImages.size();
		if (isNew) {
			info.setAuditAdminId(0L);
			info.setAuditFlag(false);
			info.setCreateTime(System.currentTimeMillis());
			info.setEditPassword("");
			info.setIp(getRealIp());
			info.setPicFlag(img_size > 0);

			info.setKeywords("");
			// info.setPicFlag(false);
			info.setUid(0L);
			info.setUpdateTime(System.currentTimeMillis());
			info.setVisitNum(0L);
		} else {
			info.setPicFlag(img_size > 0);
			info.setUpdateTime(System.currentTimeMillis());
		}

		if (info.isPicFlag()) {
			info.setPicUrl(infoImages.get(0).getSource());
		}

		infoManager.save(info);

		infoImageManager.upddateBatch(infoImages);

		// String key = (isNew) ? "info.added" : "info.updated";
		// saveMessage(getText(key));

		return SUCCESS;

	}

	private List<InfoImage> convert(List<InfoImage> infoImages) {
		if (infoImages == null) {
			return null;
		}
		List<InfoImage> tmp = new ArrayList<InfoImage>();
		for (InfoImage im : infoImages) {
			if (im.getId() != null) {
				tmp.add(im);
			}
		}
		return tmp;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	public Info getInfo() {
		return info;
	}

	public void setInfo(Info info) {
		this.info = info;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public User getInfoUser() {
		return infoUser;
	}

	public void setInfoUser(User infoUser) {
		this.infoUser = infoUser;
	}

	public List<InfoImage> getInfoImages() {
		return infoImages;
	}

	public void setInfoImages(List<InfoImage> infoImages) {
		this.infoImages = infoImages;
	}

	public void setInfoImageManager(InfoImageManager infoImageManager) {
		this.infoImageManager = infoImageManager;
	}

	public void setSearchWordManager(SearchWordManager searchWordManager) {
		this.searchWordManager = searchWordManager;
	}

	

	public Cat getFatherCat() {
		return fatherCat;
	}

	public void setFatherCat(Cat fatherCat) {
		this.fatherCat = fatherCat;
	}

	public Cat getCat() {
		return cat;
	}

	public void setCat(Cat cat) {
		this.cat = cat;
	}

	public InfoSearch getIs() {
		return is;
	}

	public void setIs(InfoSearch is) {
		this.is = is;
	}

	public List<Cat> getCats() {
		return cats;
	}

	public void setCats(List<Cat> cats) {
		this.cats = cats;
	}

	public void setCatManager(CatManager catManager) {
		this.catManager = catManager;
	}

	

	public List<Area> getAreas() {
		return areas;
	}

	public void setAreas(List<Area> areas) {
		this.areas = areas;
	}

	public void setAreaManager(AreaManager areaManager) {
		this.areaManager = areaManager;
	}

	public void setInfoManager(InfoManager infoManager) {
		this.infoManager = infoManager;
	}

	public List<Info> getInfoes() {
		return infoes;
	}

}